﻿
Partial Class WebForm_ManageVOrganization
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLActive()
            Me.BindDDLOrganizationID()
            'Me.BindOrganization()
        End If
    End Sub

    Protected Sub ddlActive_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlActive.SelectedIndexChanged
        Me.BindOrganization()
    End Sub

    Protected Sub ContextMenu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles ContextMenu1.MenuItemClick


        If e.Item.Value = "Paste" Then
            If Me.Cutting = False Then
                MyObj.ShowMessage("未選擇項目")
            Else
                Me.Cutting = False
                Me.ChangeParent(Me.trOrganization.SelectedNode.Value)
                Me.BindOrganization()
            End If

            Return
        ElseIf e.Item.Value = "Add" Then
            If Me.NodeType = NodeTypeEnum.VOrganization Then
                MyObj.ShowMessage("志工隊只能屬於單位或民間團體")
            Else
                If Me.NodeType = NodeTypeEnum.Person Then
                    Me.AddVOrganization1.Show("null", Me.VOrganizationID)
                Else
                    Me.AddVOrganization1.Show(Me.OrganizationID, "null")
                End If

                Me.TabContainer1.ActiveTab = Me.AddTabPanel

            End If

            Return
        End If

        If Me.NodeType = NodeTypeEnum.VOrganization Or Me.NodeType = NodeTypeEnum.Person Then
            If e.Item.Value = "Edit" Then
                Me.AddVOrganization1.ShowEdit(Me.VOrganizationID)
            ElseIf e.Item.Value = "Disable" Then
                Me.ChangeActiveStatus("N")
                Me.BindOrganization()
            ElseIf e.Item.Value = "Enable" Then
                Me.ChangeActiveStatus("Y")
                Me.BindOrganization()
            ElseIf e.Item.Value = "Cut" Then
                Me.Cutting = True
                Me.CuttedID = Me.VOrganizationID
            End If

            Me.TabContainer1.ActiveTab = Me.AddTabPanel
        Else
            MyObj.ShowMessage("錯誤，必須選擇志工隊")
        End If

    End Sub

    Protected Sub AddOrganization1_OrganizationAdded(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVOrganization1.OrganizationAdded
        Me.BindOrganization()
        'Me.AddData()
    End Sub

    Protected Sub AddOrganization1_OrganizationUpdated(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVOrganization1.OrganizationUpdated
        Me.UpdateData()
    End Sub

    Protected Sub AddOrganization1_Saved(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVOrganization1.Saved
        Me.TabContainer1.ActiveTab = Me.SearchTabPanel
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindOrganization()
    End Sub
End Class

Partial Class WebForm_ManageVOrganization
    Private Function AddData() As Boolean
        Dim node As New My.MyTreeNode

        node.MyText = Me.AddVOrganization1.VOrganizationName
        node.Value = Me.AddVOrganization1.VOrganizationID

        Me.trOrganization.SelectedNode.ChildNodes.Add(node)

    End Function

    Private Function UpdateData() As Boolean
        Me.BindOrganization()

    End Function

    Private Function ChangeActiveStatus(ByVal status As String) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("update VOrganization set ")
        sql.AppendFormat(" Active = '{0}' ", status)
        sql.AppendFormat("where VOrganizationID = '{0}' ", Me.VOrganizationID)

        Using scope As New Transactions.TransactionScope
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            scope.Complete()
        End Using
    End Function

    Private Function ChangeParent(ByVal parentID As String) As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("update VOrganization set ")

        If Me.NodeType = NodeTypeEnum.Organization Then
            sql.AppendFormat(" Parent = {0} ", "null")
            sql.AppendFormat(",OrganizationID = {0} ", Me.trOrganization.SelectedNode.Value)
        Else
            sql.AppendFormat(" Parent = {0} ", parentID)
            sql.AppendFormat(",OrganizationID = {0} ", "null")
        End If

        sql.AppendFormat("where VOrganizationID = '{0}' ", Me.CuttedID)

        Using scope As New Transactions.TransactionScope
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            clsAuth.BuildVOrganizationCode()
            scope.Complete()
        End Using
    End Function

    Private Sub BindDDLActive()
        Me.ddlActive.DataTextField = "Text"
        Me.ddlActive.DataValueField = "Value"
        Me.ddlActive.DataSource = MyObj.GetCodeMappingData("VOrganization", "Active")
        Me.ddlActive.DataBind()

        Me.ddlActive.Items.Insert(0, "全部")
        Me.ddlActive.SelectedIndex = Me.ddlActive.Items.IndexOf(Me.ddlActive.Items.FindByValue("Y"))
    End Sub

    Private Sub BindDDLOrganizationID()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select o.* ")
        sql.AppendFormat("from Organization o ")
        sql.AppendFormat("where Active = 'Y' ")
        'sql.AppendFormat("where o.OrganizationName like '%公所%' ")

        Me.ddlOrganizationID.DataTextField = "OrganizationName"
        Me.ddlOrganizationID.DataValueField = "OrganizationID"
        Me.ddlOrganizationID.DataSource = dat.GetData(sql)
        Me.ddlOrganizationID.DataBind()

    End Sub

    Private dtVOrganization As Data.DataTable

    Private Sub BindOrganization()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select * from VOrganization ")

        Me.dtVOrganization = dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt").Tables(0)
        Me.trOrganization.Nodes.Clear()

        Dim node As New My.MyTreeNode

        'node.Text = "Root"
        'node.Value = "null"

        node.MyText = Me.ddlOrganizationID.SelectedItem.Text
        node.Value = Me.ddlOrganizationID.SelectedValue

        node.ToolTip = node.Text

        Me.trOrganization.Nodes.Add(node)

        Me.BindOrganization(node)

        Me.trOrganization.ExpandAll()
    End Sub

    Private Sub BindOrganization(ByVal node As TreeNode)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select o.* ")
        sql.AppendFormat("from Organization o ")

        ' Root
        If node.Value = "null" Then
            sql.AppendFormat("where o.Parent is null ")
        Else
            sql.AppendFormat("where o.Parent = {0} ", node.Value)
        End If

        If Me.ddlActive.SelectedItem.Text <> "全部" Then
            sql.AppendFormat("and Active = '{0}' ", Me.ddlActive.SelectedValue)
        End If

        For Each dr As Data.DataRow In dat.GetData(sql.ToString(), Nothing, Data.CommandType.Text, "dt").Tables(0).Rows
            Dim child As New My.MyTreeNode()
            child.Value = dr("OrganizationID")

            If dr("Active") = "Y" Then
                'child.Text = String.Format("<span style='background-color:blue;color:white'>{0}</span>", dr("OrganizationName"))
                child.ForeColor = Drawing.Color.White
                child.BackColor = Drawing.Color.Blue
                child.MyText = dr("OrganizationName")
            Else
                'child.Text = String.Format("<span style='background-color:gray;color:white'>{0}</span>", dr("OrganizationName"))

                child.ForeColor = Drawing.Color.White
                child.BackColor = Drawing.Color.Gray
                child.MyText = dr("OrganizationName")
            End If

            'node.Text.Length > 0 - 單位
            'node.Text.Length = 0 - 志工隊
            child.ToolTip = child.MyText

            node.ChildNodes.Add(child)
            Me.BindOrganization(child)
        Next

        '加入志工隊 node
        Me.BindVOrganization(node)

    End Sub

    'Private Sub BindVOrganization()

    '    Me.trOrganization.Nodes.Clear()

    '    Dim node As New My.MyTreeNode

    '    node.MyText = "Root"
    '    node.Value = "null"

    '    Me.trOrganization.Nodes.Add(node)

    '    Me.BindVOrganization(node)

    '    Me.trOrganization.ExpandAll()
    'End Sub

    Private Sub BindVOrganization(ByVal node As TreeNode)
        Dim sql As New MyStringBuilder

        ' node 為單位
        'node.Text.Length > 0 - 單位
        'node.Text.Length = 0 - 志工隊
        If node.Value = "null" Or node.ToolTip.Length > 0 Then
            sql.AppendFormat("[Parent] is null and OrganizationID = {0} ", node.Value)
        Else
            sql.AppendFormat("[Parent] = {0}", node.Value)
        End If

        If Me.ddlActive.SelectedItem.Text <> "全部" Then
            sql.AppendFormat("and Active = '{0}' ", Me.ddlActive.SelectedValue)
        End If

        Me.dtVOrganization.DefaultView.RowFilter = sql.ToString()

        For Each dr As Data.DataRowView In Me.dtVOrganization.DefaultView
            Dim child As New My.MyTreeNode()

            child.Value = dr("VOrganizationID")

            If dr("OrganizationType") = "P" Then
                dr("VOrganizationName") = dr("VOrganizationName").ToString() + " (民間)"
            End If
            
            If dr("Active") = "Y" Then
                child.MyText = String.Format("{0}", dr("VOrganizationName"))
            Else
                child.ForeColor = Drawing.Color.Gray
                child.BackColor = Drawing.Color.White
                child.MyText = String.Format("{0}", dr("VOrganizationName"))
            End If

            node.ChildNodes.Add(child)
            Me.BindVOrganization(child)
        Next

    End Sub

    Private ReadOnly Property VOrganizationID() As String
        Get
            Return Me.trOrganization.SelectedNode.Value
        End Get
    End Property

    Private ReadOnly Property OrganizationID() As String
        Get
            Return Me.trOrganization.SelectedNode.Value
        End Get
    End Property

    Private Property Cutting() As Boolean
        Get
            Return ViewState("Cutting")
        End Get
        Set(ByVal value As Boolean)
            ViewState("Cutting") = value
        End Set
    End Property

    Private Property CuttedID() As String
        Get
            Return ViewState("CuttedID")
        End Get
        Set(ByVal value As String)
            ViewState("CuttedID") = value
        End Set
    End Property

    Private Function GetNodeType(ByVal node As TreeNode) As NodeTypeEnum
        ' node 為單位
        If node.Value = "null" Or node.ToolTip.Length > 0 Then
            Return NodeTypeEnum.Organization
        ElseIf node.Text.Contains("民間") Then
            Return NodeTypeEnum.Person
        Else
            Return NodeTypeEnum.VOrganization
        End If

    End Function

    Private ReadOnly Property NodeType() As NodeTypeEnum
        Get
            Return Me.GetNodeType(Me.trOrganization.SelectedNode)
        End Get
    End Property

    Private ReadOnly Property IsRoot() As Boolean
        Get
            ' node 為單位
            If Me.trOrganization.SelectedNode.Value = "null" Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property


    Private Enum NodeTypeEnum
        Organization
        VOrganization
        Person
    End Enum
End Class